package uit.nfcmenu.manager;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.TextView;

public class AssetsManager {
	static String tag = AssetsManager.class.getSimpleName();
	
	public static Drawable getDrawable(Context context, String imageName) {
		AssetManager am = context.getAssets();
		Drawable drawable = null;
		try {
			drawable = Drawable.createFromStream(
					am.open("images/" + imageName), null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return drawable;
	}
	
	public static void setFont(Context context, TextView txt, String font) {
		if (txt != null) {
			String suffix = font.substring(font.length()-4, font.length());
			if (!suffix.equalsIgnoreCase(".ttf") && !suffix.equalsIgnoreCase(".otf")) {
				font += ".ttf";
			}
			font = "font/" + font;
			try {
				Typeface typeface = Typeface.createFromAsset(context.getAssets(), font);  
				txt.setTypeface(typeface);
			} catch (Exception e) {
				Log.e(tag, "font not found in assets: "+font);
			}
			//Log.i(tag, "setFont(): font="+font);
		}
	}
	
	public static void setFont(Context context, int txtId, String font) {
		TextView txt = (TextView) ((Activity) context).findViewById(txtId);
		setFont(context, txt, font);
	}
	
	public static void setCommonAppFont(Context context, TextView txt) {
		if (txt != null) {
			Typeface typeFace = txt.getTypeface();
			
	//		String font = "BellGothicStd-Bold.otf";// wrong// font from Graphic
			
	//		String font = "cinnamon cake.ttf"; // cate wrong
	//		String font = "VNI-Diudang.ttf"; // vni wrong
	//		String font = "comic.ttf"; // unicode wrong
	//		String font = (typeFace != null && typeFace.isBold()) 
	//				? "comicbd.ttf" : "comic.ttf";
			
			// can view Vietnamese with unicode font
	//		String font = "windsorb.ttf";
	//		String font = "UVNDaLat_R.TTF"; 
	//		String font = "aachenb.ttf";
	//		String font = "amazone.ttf";
			
//			String font = (typeFace == null || !typeFace.isBold()) 
//					? "tahoma.ttf" : "tahomabd.ttf";
			String font = (typeFace == null || !typeFace.isBold()) 
					? "UVNBanTay.TTF" : "UVNDaLat_R.TTF";
			setFont(context, txt, font);		
		}
	}
	
	public static void setCommonAppFont(Context context, int txtId) {
		TextView txt = (TextView) ((Activity) context).findViewById(txtId);
		setCommonAppFont(context, txt);
	}
}
